﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitView;
using RevitInteractiveUtility.ExternalEventManager;

namespace YJKColumnModify.ConnectColumnBream
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdConnectColumnBream : IExternalCommand
	{
		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			WindowHandle hWndHandle = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			Document document = commandData.Application.ActiveUIDocument.Document;
			this.m_commandData = commandData;
			Result result;
			try
			{
				CmdViewType types = (CmdViewType)9;
                if (YJKRevitView.SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					this.m_selectBream = new SelectBream(commandData);
					List<BreamInfo> list = new List<BreamInfo>();
					List<BreamInfo> lstBreamInfoOfSelect = new List<BreamInfo>();
					int needSelectWay = this.GetNeedSelectWay(ref list, hWndHandle);
					if (needSelectWay == -1)
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
					else
					{
						if (needSelectWay != 2)
						{
							lstBreamInfoOfSelect = list;
						}
						else if (!this.BreamSelectLevel(list, ref lstBreamInfoOfSelect, hWndHandle))
						{
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						ExternalEventManagerSingleton.GetEventManger(false).ReSetRequest();
						this.ShowNeedConnectResults(lstBreamInfoOfSelect, hWndHandle);
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		private int GetNeedSelectWay(ref List<BreamInfo> lstInfo, WindowHandle hWndHandle)
		{
			ObjectstartForm objectstartForm = new ObjectstartForm(hWndHandle);
			if (objectstartForm.ShowDialog(hWndHandle) == DialogResult.OK)
			{
				int theSelect = objectstartForm.TheSelect;
				if (theSelect == 0)
				{
					lstInfo.Clear();
					this.m_selectBream.GetBreamOfSelect(ref lstInfo);
				}
				if (theSelect == 1)
				{
					lstInfo.Clear();
					this.m_selectBream.GetBreamOfLevel(ref lstInfo);
				}
				if (theSelect == 2)
				{
					lstInfo.Clear();
					this.m_selectBream.GetBreamOfBuild(ref lstInfo);
				}
				objectstartForm.Close();
				return theSelect;
			}
			return -1;
		}

		private bool BreamSelectLevel(List<BreamInfo> lstAllInfo, ref List<BreamInfo> lstBreamInfoOfSelect, WindowHandle hWndHandle)
		{
			if (lstAllInfo.Count == 0)
			{
				YJKMessageBox.Show("没有需要连接的梁", MessageBoxButtons.OK);
				return false;
			}
			SelectLevelForm selectLevelForm = new SelectLevelForm(lstAllInfo, hWndHandle);
			if (selectLevelForm.ShowDialog(hWndHandle) == DialogResult.OK)
			{
				lstBreamInfoOfSelect = selectLevelForm.SelectBreamInfo;
				selectLevelForm.Close();
				return true;
			}
			selectLevelForm.Close();
			return false;
		}

		private bool ShowNeedConnectResults(List<BreamInfo> lstBreamInfoOfSelect, WindowHandle hWndHandle)
		{
			Dictionary<string, List<ConnectColumnBreamInfo>> dictionary = new Dictionary<string, List<ConnectColumnBreamInfo>>();
			dictionary = new AnalyzeBream(this.m_commandData, lstBreamInfoOfSelect).GetConnectColumnBreams();
			if (dictionary.Count == 0)
			{
				YJKMessageBox.Show("没有需要连接的梁", MessageBoxButtons.OK);
				return false;
			}
			new GeneralCheckResultForm(dictionary, hWndHandle).Show(hWndHandle);
			return true;
		}

		private SelectBream m_selectBream;

		private ExternalCommandData m_commandData;
	}
}
